package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @package com.leyou.item.web
 * @description: 商品服务的controller层
 * @author: 许超越
 * @date 2019/6/28 11:01
 * @version: V1.0
 */
@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询商品列表的分页
     * @param: page 当前页
     * @param: rows 每页显示的行数
     * @param: saleable 是否已经上架
     * @param: key 搜索商品的关键字
     * @return
     */
    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "key",required = false) String key
    ){
        return ResponseEntity.ok(goodsService.querySpuByPage(page, rows, saleable, key));
    }


    /**
     * 新增商品
     * @param spuDTO 页面提交商品信息
     * @return
     */
    @PostMapping("goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    /**
     * 根据spuId更新上下架状态
     * @param: spuId
     * @return
     */
    @PutMapping("spu/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id") Long spuId, @RequestParam("saleable") Boolean saleable) {
        goodsService.updateSpuSaleable(spuId, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 通过spuId查询spuDetail数据
     * @param: spuId
     * @return
     */
    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long spuId) {
        return ResponseEntity.ok(goodsService.querySpuDetailById(spuId));
    }


    /**
     * 通过spuId查询sku数据
     * @param: spuId
     * @return
     */
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("id") Long spuId) {
        return ResponseEntity.ok(goodsService.querySkuBySpuId(spuId));
    }

    /**
     * 更新商品
     * @param: spuDTO 更新商品的数据
     * @return
     */
    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据spuId查询spu
     * @param: spuId
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long spuId) {
        return ResponseEntity.ok(goodsService.querySpuById(spuId));
    }


    /**
     * 根据skuId集合查询sku
     * @param ids
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuByIds(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(goodsService.querySkuByIds(ids));
    }


    /**
     * 减库存
     * @param cartMap 商品id及数量的map
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> cartMap){
        goodsService.minusStock(cartMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    /**
     * 增加库存
     * @param skuMap 商品id和商品数量
     * @return
     */
    @PutMapping("/stock/plus")
    public ResponseEntity<Void> plusStock(@RequestBody Map<Long, Integer> skuMap) {
        goodsService.plusStock(skuMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
